/**
 * 
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.IssueBOInterface;
import org.cuatrovientos.erp.model.Issue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Raquel Valenzuela <raquel_valenzuela@cuatrovientos.es>
 * 
 */
public class IssueController {
	// Get log4j logger
	private static final Logger logger = Logger.getLogger(EmployeeController.class);

	@Autowired
	private IssueBOInterface issueBO;

	/**
	 * default constructor
	 */
	public IssueController() {
		logger.info("issue BEAN instantiated");

	}

	/**
	 * handles default /issue or /issue/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issue","/issue/list" )
	 */
	@RequestMapping({ "/issue", "/issue/list" })
	public ModelAndView issueDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Issue> issueList = issueBO.selectAll();
		model.put("issues", issueList);
		return new ModelAndView("issueList", model);
	}

	/**
	 * handles /issue/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issue/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issue/search" })
	public ModelAndView search(
			@RequestParam(value = "description", defaultValue = "", required = true) String description) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Issue> issueList = issueBO.search(description);
		model.put("issues", issueList);

		return new ModelAndView("issueList", model);
	}

	/**
	 * handles delete issue, and redirect to issue default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issue","/issue/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issue/delete" })
	public String deleteIssue(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		issueBO.deleteById(id);
		return "redirect:/issue";

	}

	/**
	 * handles new issue request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issue/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issue/new" })
	public ModelAndView newIssue() {
		logger.info("new issue ");
		Issue issue = new Issue();

		return new ModelAndView("IssueNew", "issue", issue);
	}

	/**
	 * handles new issue request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issue/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issue/save" })
	public String newIssue(@Valid Issue issue,
			BindingResult bindingResult) {
		logger.info("new issue " + issue.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issue", issue);
			return "IssueNew";
		}

		// If data is ok, insert and go on
		issueBO.insert(issue);
		return "redirect:/issue";
	}

	/**
	 * handles delete issue, and redirect to issue default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issue","/issue/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issue/detail" })
	public ModelAndView issueDetail(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Issue issue = issueBO.selectById(id);
		model.put("issue", issue);

		return new ModelAndView("IssueDetail", model);

	}

	/**
	 * handles issue update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/issue/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/issue/update" })
	public ModelAndView issueUpdate(
			@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Issue issue = issueBO.selectById(id);
		model.put("issue", issue);
		return new ModelAndView("IssueUpdate", model);

	}

	/**
	 * handles save update issue request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/issue/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/issue/saveupdate" })
	public String saveUpdate(@Valid Issue issue,
			BindingResult bindingResult) {
		logger.info("update issue " + issue.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issue", issue);
			return "IssueUpdate";
		}

		issueBO.update(issue);
		return "redirect:/issue";
	}
}
